﻿using System;
using System.Collections;
using System.Data;
using Lib.Data;
using Lib.Utils;


namespace SysObj
{
    public class Bg_GiaNhap : Lib.Bases.BaseClass
    {
        SysObj.Bg_GiaNhap_ChiTiet _detail = new Bg_GiaNhap_ChiTiet();

        #region Prop
        public System.Int32 IdGiaNhap
        {
            get;
            set;
        }

        public System.DateTime Ngay
        {
            get;
            set;
        }

        public System.String GhiChu
        {
            get;
            set;
        }

        #endregion

        public DataTable D_DT { get; set; }

        #region  Constructors
        public Bg_GiaNhap()
        {
            m_DataLayer = new Lib.Data.DataLayer();
        }
        #endregion

        #region Methods
        public override void ResetData()
        {
            IdGiaNhap = 0;
            Ngay = DateTime.Now;
            GhiChu = string.Empty;
        }

        public void ReadData(System.Int32 v_IdGiaNhap)
        {
            DataTable dt = new DataTable();
            dt = m_DataLayer.ExecDataTable("SP_Bg_GiaNhap", " Command,IdGiaNhap", "ReadData", v_IdGiaNhap);
            this.ResetData();
            if (dt == null || dt.Rows.Count <= 0)
                return;
            this.FillData(dt.Rows[0]);
        }

        public void ReadData(DateTime Date)
        {
            DataTable dt = new DataTable();
            dt = m_DataLayer.ExecDataTable("SP_Bg_GiaNhap", " Command,Ngay", "ReadDataByDate", Cvert.ToShortDate(Date));
            this.ResetData();
            if (dt == null || dt.Rows.Count <= 0)
                return;
            this.FillData(dt.Rows[0]);
        }

        public override DataTable LoadData()
        {
            return m_DataLayer.ExecDataTable("SP_Bg_GiaNhap", " Command", "LoadData");
        }

        public DataTable GetDetail()
        {
            return _detail.GetDetail(m_DataLayer, IdGiaNhap);
        }

        public DataTable GetHistory()
        {
            return m_DataLayer.ExecDataTable("SP_Bg_GiaNhap", " Command", "GetHistory");
        }

        public override void FillData(DataRow row)
        {
            ResetData();
            if (row.Table.Columns.Contains("IdGiaNhap"))
                IdGiaNhap = Lib.Data.ValueType.GetValue(row["IdGiaNhap"], IdGiaNhap);
            if (row.Table.Columns.Contains("Ngay"))
                Ngay = Lib.Data.ValueType.GetValue(row["Ngay"], Ngay);
            if (row.Table.Columns.Contains("GhiChu"))
                GhiChu = Lib.Data.ValueType.GetValue(row["GhiChu"], GhiChu);
        }

        public override void OnUpdate()
        {
            Hashtable output = null;
            string s_ParamName = "";
            object[] o_ParamValue = null;
            try
            {
                m_DataLayer.BeginTransaction();

                Lib.Data.ParamHolder.ClearAll();
                Lib.Data.ParamHolder.AddParam("Command", "Update");
                Lib.Data.ParamHolder.AddParam("IdGiaNhap", IdGiaNhap);
                Lib.Data.ParamHolder.AddParam("Ngay", Cvert.ToShortDate(Ngay));
                Lib.Data.ParamHolder.AddParam("GhiChu", GhiChu);
                Lib.Data.ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
                m_DataLayer.ExecNonQuery("SP_Bg_GiaNhap", s_ParamName, ref output, o_ParamValue);

                if (!object.Equals(null, output))
                    IdGiaNhap = Cvert.ToInt32(output["IdGiaNhap"], 0);

                _detail.OnDelete(m_DataLayer, IdGiaNhap);
                _detail.Insert(m_DataLayer, D_DT, IdGiaNhap);

                m_DataLayer.CommitTransaction();
            }
            catch (Exception ex)
            {
                m_DataLayer.RollbackTransaction();
                throw ex;
            }
        }

        public override void OnDelete()
        {
            string s_ParamName = "";
            object[] o_ParamValue = null;
            try
            {
                m_DataLayer.BeginTransaction();

                _detail.OnDelete(m_DataLayer, IdGiaNhap);

                Lib.Data.ParamHolder.ClearAll();
                Lib.Data.ParamHolder.AddParam("Command", "Delete");
                Lib.Data.ParamHolder.AddParam("IdGiaNhap", IdGiaNhap);
                Lib.Data.ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
                m_DataLayer.ExecNonQuery("SP_Bg_GiaNhap", s_ParamName, o_ParamValue);

                m_DataLayer.CommitTransaction();
            }
            catch (Exception ex)
            {
                m_DataLayer.RollbackTransaction();
                throw ex;
            }
        }


        #endregion
    }
}
